(function(){
	
	if (!window.CJ) {
		window.CJ = {};
	}

	function Promotion () {
		this.name = 'Promotion';
		this.bindEvent();
	}
	
	Promotion.prototype = new EventCtl();
	Promotion.prototype.bindEvent = function () {
		$('#global .event').on('click', function() {
			CJ.Scene.promotionMenu();
		});
		$('#global .event').on('mouseenter', function() {
			if(!CJ.isRightDetailEntry) {
				$('#global li').removeClass('focus');
				$('#global .event').addClass('focus');
				CJ.Scene.setDelegate(CJ.Scene["Promotion"]);
			}
		});
	};
	Promotion.prototype.down = function () {
		CJ.Scene.banner();
	};
	Promotion.prototype.left = function () {
		CJ.Scene.search();
	};
	Promotion.prototype.right = function () {
	};
	Promotion.prototype.back = function () {
		$('#global li').removeClass('focus');
		$("#event").hide();
		$("#mask").hide();
		CJ.Scene.channel();
	};
	Promotion.prototype.enter = function () {
		CJ.Scene.promotionMenu();
	};
	
	
	
	function PromotionMenu () {
		this.name = 'PromotionMenu';
		this.rootID = '#event';
		this.rootElem = $(this.rootID + ' .tab');
		this.bindEvent();
	}
	PromotionMenu.prototype = new EventCtl();
	PromotionMenu.prototype.bindEvent = function () {
		var self = this;
		$(this.rootElem).on('click', 'li',  function() {
			var menu = $(this).addClass('on').siblings().removeClass('on').end().data('menu');
			$(self.rootID).removeClass().addClass(menu);
			CJ.Scene.setDelegate(CJ.Scene["PromotionMenu"]);
		});
		$(this.rootElem).on('mouseenter', 'li', function() {
			$(this).addClass('focus').siblings().removeClass('focus');
			$(self.rootID + '>div .focus').removeClass('focus');
			CJ.Scene.setDelegate(CJ.Scene["PromotionMenu"]);
		});
	};
	
	PromotionMenu.prototype.left = function () {
		this.moveH('prev');
	};
	PromotionMenu.prototype.right = function () {
		this.moveH('next');
	};
	PromotionMenu.prototype.moveH = function (func) {
		var idx, now = $('li.on', this.rootElem),
			next = now[func]();
		if (next.length>0) {
			$('li.focus', this.rootElem).removeClass('focus');
			now.removeClass('on');
			next.addClass('on focus');
			$(this.rootID).removeClass().addClass(next.data('menu'));
		}
		//this.enter();
	};
	PromotionMenu.prototype.down = function () {
		this.enter();
	};
	PromotionMenu.prototype.back = function () {
		CJ.Scene.promotion();
	};
	PromotionMenu.prototype.enter = function () {
		var menu = $("#event .tab li.on").removeClass('focus').data('menu');
		if (menu == 'dep1') {
			CJ.Scene.eventList();
		} else if (menu == 'dep2') {
			CJ.Scene.promotionList();
		} else {
			CJ.Scene.expiredList();
		}//
	};
	
	
	function EventList (wrap) {
		this.rootID = wrap;
		this.rootElem = $(wrap);
		this.listElem = $(wrap + '>.itemList');
		this.countElem = $(wrap + ' .count');
		this.prevBtnElem = $(wrap + '>.btn_prev');
		this.nextBtnElem = $(wrap + '>.btn_next');
		this.lists = [];
		this.totalCount = 0;
		this.pageSize = 10;
		this.pageNo = 0;
		this.renderStartIdx = 0;
		this.renderRange = 3;
		if (wrap == '#eventWrap') {
			this.name = 'EventList';
			this.type = 'event';
			this.ajaxUrl = 'EVENTLIST';
			this.itemTag = 'dl';
			this.template = $.render.eventListTemplate;
		} else if (wrap == '#promotionWrap') {
			this.name = 'PromotionList';
			this.type = 'promotion';
			this.ajaxUrl = 'PROMOTIONLIST';
			this.itemTag = 'dl';
			this.template = $.render.promotionListTemplate;
		} else {
			this.name = 'ExpiredList';
			this.ajaxUrl = 'ENDEVENTLIST';
			this.itemTag = 'li';
			this.template = $.render.expiredListTemplate;
			this.renderRange = 4;
		}
		this.bindEvent();
	}
	
	EventList.prototype = new EventCtl();
	EventList.prototype.bindEvent = function () {
		var self = this;
		this.prevBtnElem.on('click', function() {
			self.enter();
		});
		this.nextBtnElem.on('click', function() {
			self.enter();
		});
		this.rootElem.on('click', this.itemTag, function() {
			self.enter();
		});
		
		this.rootElem.on('mouseenter', this.itemTag, function() {
			$('#event .tab .focus').removeClass('focus');
			self.prevBtnElem.removeClass('focus');
			self.nextBtnElem.removeClass('focus');
			$(this).addClass('focus').siblings('.focus').removeClass('focus');
			CJ.Scene.setDelegate(CJ.Scene[self.name]);
		});
	
		this.prevBtnElem.on('mouseenter', function () {
			$('#event .tab .focus').removeClass('focus');
			$('.focus', self.rootElem).removeClass('focus');
			$(this).addClass('focus');
			CJ.Scene.setDelegate(CJ.Scene[self.name]);
		});
	
		this.nextBtnElem.on('mouseenter', function () {
			$('#event .tab .focus').removeClass('focus');
			$('.focus', self.rootElem).removeClass('focus');
			$(this).addClass('focus');
			CJ.Scene.setDelegate(CJ.Scene[self.name]);
		});
	
	};
	EventList.prototype.initData = function (succCB) {
		if (this.totalCount == 0) {
			this.getData(succCB);
		}
	};
	EventList.prototype.getData = function (succCB) {
		var self = this;
		CJ.HttpJson.get({
			type: this.ajaxUrl,
			param: {
				pageSize: this.pageSize,
				pageNo: this.pageNo+1
			},
			success: function (json) {
				self.successCB(json);
				if (typeof succCB == 'function') {
					succCB();
				}
			}
		});
	};
	EventList.prototype.successCB = function (json) {
		this.lists  = this.lists.concat(json.eventlist);
		this.totalCount = parseInt(json.total_count);
		this.totalPage =  parseInt(json.page_info.total_page);
		this.pageNo =  parseInt(json.page_info.page);
		if (json.page_info.page == 1) {
			this.render();
			this.countElem.text(this.totalCount);
		}
	};
	EventList.prototype.getMore = function () {
		if (this.totalPage != this.pageNo) {
			this.getData();
		}
	};
	EventList.prototype.render = function (sIdx) {
		sIdx = sIdx || 0;
		var len = this.lists.length;
		var range = this.renderRange;
		var arr = [];
		if (len < (range+sIdx) ){
			range = len - sIdx;
		}
		
		for (var i=0; i<range; i++) {
			arr.push(this.lists[i+sIdx]);
		}
		this.renderStartIdx = sIdx ;
		
		$( this.listElem ).html(
			this.template( arr )
		);
		this.pageNum(sIdx);
	};
	EventList.prototype.pageNum = function(idx) {
		var total = this.totalCount;
		$(this.rootID+' .btn_list>span').text(parseInt(idx+1)+'/'+total);
	};
	EventList.prototype.extend = function() {
		this.getMore();
	};
	EventList.prototype.left = function () {
		this.moveH('left');
	};
	EventList.prototype.right = function () {
		this.moveH('right');
	};
	EventList.prototype.moveH = function (dirc) {
		var now = $('.focus', this.rootElem);
		if (now[0].tagName == this.itemTag.toUpperCase()) {
			now.removeClass('focus');
			if (dirc == 'left') {
				this.prevBtnElem.addClass('focus');
			} else {
				this.nextBtnElem.addClass('focus');
			}
		} else if ((now.hasClass('btn_prev') && dirc == 'right') ||  (now.hasClass('btn_next') && dirc == 'left')) {
			now.removeClass('focus');
			$('.on', this.rootElem).addClass('focus');
		}
	};
	EventList.prototype.up = function () {
		this.moveV('prev');
	};
	EventList.prototype.down = function () {
		this.moveV('next');
	};
	EventList.prototype.moveV = function (downup) {
		this.extend();
		var now = $('.focus', this.rootElem);
		var next = now[downup]();
		if (now[0].tagName == this.itemTag.toUpperCase() && next.length>0) {
			now.removeClass('on focus');
			next.addClass('on focus');
		} else if (downup == 'prev') {
			now.removeClass('focus');
			CJ.Scene.promotionMenu();
		}
	};
	EventList.prototype.back = function () {
		$('.focus', this.rootElem).removeClass('focus');
		CJ.Scene.promotionMenu();
	};
	EventList.prototype.enter = function () {
		var self = this;
		var sIdx;
		var now = $(".focus", this.rootElem);
		if (now.hasClass('btn_list')) {
			if(now.hasClass('btn_prev')) {
				sIdx = this.renderStartIdx - this.renderRange;
			} else {
				sIdx = this.renderStartIdx + this.renderRange;
			}
			
			if(sIdx >= 0 && sIdx < this.lists.length) {
				this.render(sIdx);
			}
		} else {
			var eventid = now.data('eventid');
			if (this.name == 'ExpiredList') {
				console.log('당첨자 보기');
				CJ.Scene.resultList(eventid);
			} else {
				console.log('이벤트 보기');
				CJ.Scene.popupView(this.type, eventid,
					function(){ //이벤트 조회 성공 콜백
						$('#event').hide();
					},
					function(){//이벤트 창이 닫힐 때 콜백
						$('#event').show();
						CJ.Scene.setDelegate(CJ.Scene[self.name]);
					});
			}
		}
	};
	
	
	//당첨자 보기 Control
	function ResultList () {
		this.name = 'ResultList';
		this.rootID = '#event>.result';
		this.rootElem = $(this.rootID);
		this.titleElem = $(this.rootID + '>h3');
		this.listElem = $(this.rootID + '>ul.mem');
		this.btnElem = $(this.rootID + '>ul.btn>li');
		this.prevBtnElem = $(this.rootID + '>.btn_prev');
		this.nextBtnElem = $(this.rootID + '>.btn_next');
		this.lists = [];
		this.totalCount = 0;
		this.pageSize = 10;
		this.pageNo = 0;
		this.renderStartIdx = 0;
		this.renderRange = 12;
		this.bindEvent();
	}
	
	ResultList.prototype = new EventCtl();
	ResultList.prototype.bindEvent = function () {
		var self = this;
		this.prevBtnElem.on('click', function() {
			self.enter();
		});
		this.nextBtnElem.on('click', function() {
			self.enter();
		});
		this.btnElem.on('click', this.itemTag, function() {
			self.enter();
		});
		
		this.btnElem.on('mouseenter', this.itemTag, function() {
			$('#event .tab .focus').removeClass('focus');
			self.prevBtnElem.removeClass('focus');
			self.nextBtnElem.removeClass('focus');
			$(this).addClass('focus');
			CJ.Scene.setDelegate(CJ.Scene[self.name]);
		});
	
		this.prevBtnElem.on('mouseenter', function () {
			$('#event .tab .focus').removeClass('focus');
			$('.focus', self.rootElem).removeClass('focus');
			$(this).addClass('focus');
			CJ.Scene.setDelegate(CJ.Scene[self.name]);
		});
	
		this.nextBtnElem.on('mouseenter', function () {
			$('#event .tab .focus').removeClass('focus');
			$('.focus', self.rootElem).removeClass('focus');
			$(this).addClass('focus');
			CJ.Scene.setDelegate(CJ.Scene[self.name]);
		});
	
	};
	ResultList.prototype.initData = function (id) {
		if (this.totalCount == 0) {
			this.eventId = id;
			this.getData();
		}
	};
	ResultList.prototype.getData = function () {
		var self = this;
		CJ.HttpJson.get({
			type: 'ANNOUNCELIST',
			param: {
				AnnounceID: this.eventId,
				pageSize: this.pageSize,
				pageNo: this.pageNo+1
			},
			success: function (json) {
				self.successCB(json);
			}
		});
	};
	ResultList.prototype.successCB = function (json) {
		this.lists  = this.lists.concat(json.announcelist);
		this.totalCount = parseInt(json.total_count);
		this.totalPage =  parseInt(json.page_info.total_page);
		this.pageNo =  parseInt(json.page_info.page);
		if (json.page_info.page == 1) {
			this.render();
			this.titleElem.html(
				json.announcetitle + '<br> 이벤트 기간 : ' + CJ.util.getDateStr(json.starttime, '.') +'~'+ CJ.util.getDateStr(json.endtime, '.')
			);
		}
	};
	ResultList.prototype.getMore = function () {
		if (this.totalPage != this.pageNo) {
			this.getData();
		}
	};
	ResultList.prototype.render = function (sIdx) {
		function phoneNum(n) {
			return n.substr(0,3) + " - " + n.substr(4, 4) + " - ****";
		}
		sIdx = sIdx || 0;
		var len = this.lists.length;
		var range = this.renderRange;
		var arr = [];
		var docfrag = document.createDocumentFragment();
		var obj, li;
		
		if (len < (range+sIdx) ){
			range = len - sIdx;
		}
		
		for (var i=0; i<range; i++) {
			obj = this.lists[i+sIdx];
			li = document.createElement('li');
			li.innerText = obj.username.substr(0,1) + '**   ' + phoneNum(obj.usertel);
			docfrag.appendChild(li);
		}
		
		this.renderStartIdx = sIdx;
		this.listElem[0].innerHTML = '';
		this.listElem[0].appendChild(
			docfrag
		);
		this.pageNum(sIdx);
	};
	ResultList.prototype.pageNum = function(idx) {
		var total = this.totalCount;
		$(this.rootID+' .btn_list>span').text(parseInt(idx+1)+'/'+total);
	};
	ResultList.prototype.extend = function() {
		this.getMore();
	};
	ResultList.prototype.left = function () {
		$('.focus', this.rootElem).removeClass('focus');
		this.prevBtnElem.addClass('focus');
	};
	ResultList.prototype.right = function () {
		$('.focus', this.rootElem).removeClass('focus');
		this.nextBtnElem.addClass('focus');
	};
	// ResultList.prototype.up = function () {
		// $('.focus', this.rootElem).removeClass('focus');
		// CJ.Scene.expiredList(true);
		// CJ.Scene.promotionMenu();
	// };
	ResultList.prototype.down = function () {
		$('.focus', this.rootElem).removeClass('focus');
		this.btnElem.addClass('focus');
	};
	ResultList.prototype.back = function () {
		CJ.Scene.expiredList(true);
	};
	ResultList.prototype.enter = function () {
		var sIdx;
		var now = $(".focus", this.rootElem);
		if (now.hasClass('btn_list')) {
			if(now.hasClass('btn_prev')) {
				sIdx = this.renderStartIdx - this.renderRange;
			} else {
				sIdx = this.renderStartIdx + this.renderRange;
			}
			if(sIdx >= 0 && sIdx < this.lists.length) {
				this.render(sIdx);
			}
		} else {
			console.log('당첨자 이벤트 목록 보기');
			CJ.Scene.expiredList(true);
		}
	};
	

	$.extend(CJ.Scene, {
							promotion: function (){
								CJ.isRightDetailEntry = false;
								$("#wrap").removeClass().addClass('ch');//vod 화면 전환
								$('#global li').removeClass('focus');
								$('#global .event').addClass('focus');
								$("#event").hide();
								$("#mask").hide();
								this.setDelegate(this["Promotion"]);
							},
							promotionMenu: function (){
								var self = this;
								CJ.isRightDetailEntry = true;
								$("#wrap").removeClass().addClass('ch');//vod 화면 전환
								$('#global li').removeClass('focus');
								$("#event .tab li.on").addClass('focus');
								$("#event").show();
								$("#mask").show();
								self["EventList"].initData(function (){
									self["PromotionList"].initData(function(){
										self["ExpiredList"].initData();
									});
								});
								this.setDelegate(this["PromotionMenu"]);
							},
							eventList: function (){
								$('.on, .focus', this["EventList"].listElem).removeClass('on focus');
								$('dl:first-child', this["EventList"].listElem).addClass('on focus');
								this.setDelegate(this["EventList"]);
							},
							promotionList: function (){
								$('.on, .focus', this["PromotionList"].listElem).removeClass('on focus');
								$('dl:first-child', this["PromotionList"].listElem).addClass('on focus');
								this.setDelegate(this["PromotionList"]);
							},
							expiredList: function (isFromResult){
								if (!isFromResult) {	//텝 메뉴에서 진입한 경우
									$('.on, .focus', this["ExpiredList"].listElem).removeClass('on focus');
									$('li:first-child', this["ExpiredList"].listElem).addClass('on focus');
								} else {	//당첨자 보기 목록에서 진입한 경우
									$('#event').removeClass().addClass('dep3');
								}
								this.setDelegate(this["ExpiredList"]);
							},
							resultList: function (eventid){
								this["ResultList"].initData(eventid);
								$('#event').removeClass().addClass('result');
								this["ResultList"].rootElem.find('.focus').removeClass('focus');
								this["ResultList"].btnElem.addClass('focus');
								this.setDelegate(this["ResultList"]);
							}
						});

	$(document).ready(function(){
		CJ.Scene["Promotion"] = new Promotion();
		CJ.Scene["PromotionMenu"] = new PromotionMenu();
		CJ.Scene["EventList"] = new EventList('#eventWrap');
		CJ.Scene["PromotionList"] = new EventList('#promotionWrap');
		CJ.Scene["ExpiredList"] = new EventList('#expiredWrap');
		CJ.Scene["ResultList"] = new ResultList();
	});

})();